/**
  ******************************************************************************
  * File Name          : USART.c
  * Description        : This file provides code for the configuration
  *                      of the USART instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "usart.h"

/* USER CODE BEGIN 0 */

#include "stdlib.h"
#include "stdarg.h"
#include "string.h"  
#include "shell.h"

extern struct shell_input usart1_shell;
extern void usart1_puts(char * data , uint16_t len);
extern int isAllNum(char *str);

char Uart1_RxBuff[256] = {0};
uint16_t Uart1_Rx_Cnt = 0;
volatile uint16_t Usart1_Rec_OK = 0;//接收完整一包数据


/* USER CODE END 0 */

UART_HandleTypeDef huart1;

/* USART1 init function */

void MX_USART1_UART_Init(void)
{

  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */

  /* USER CODE END USART1_MspInit 0 */
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();
  
    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration    
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspInit 1 */

  /* USER CODE END USART1_MspInit 1 */
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspDeInit 0 */

  /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();
  
    /**USART1 GPIO Configuration    
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX 
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

    /* USART1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspDeInit 1 */

  /* USER CODE END USART1_MspDeInit 1 */
  }
} 

/* USER CODE BEGIN 1 */

/* 虚拟示波器 */
char s[22]={'b','y',16,6};
void send_wave(float arg1,float arg2,float arg3,float arg4){
    s[20]='\r';
    s[21]='\n';
    memcpy(s+4,&arg1,sizeof(arg1));
    memcpy(s+8,&arg2,sizeof(arg1));
    memcpy(s+12,&arg3,sizeof(arg1));
    memcpy(s+16,&arg4,sizeof(arg1));
    HAL_UART_Transmit(&huart1,(uint8_t *)s,sizeof(s),2000);
    }

/* 自写串口输出函数 */
void Uart_Printf(char *fmt,...)
{
va_list ap;//初始化指向可变参数列表的指针       
char string[256];       
va_start(ap,fmt);//将第一个可变参数的地址付给ap，即ap指向可变参数列表的开始       
vsprintf(string,fmt,ap);//将参数fmt、ap指向的可变参数一起转换成格式化字符串，放string数组中       
HAL_UART_Transmit(&huart1,string,strlen(string),0xffff); 
va_end(ap);    //ap付值为0，没什么实际用处，主要是为程序健壮性  
}

/* 改写串口发送函数,用于命令终端 */
void usart1_puts(char * data , uint16_t len){
	HAL_UART_Transmit(&huart1,(uint8_t*)data,len,1000);
}

/* 串口中断回调函数 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
  if(huart->Instance==USART1)
       HAL_UART_Receive_IT(&huart1,(uint8_t*)&Uart1_RxBuff[++Uart1_Rx_Cnt], 1);         
}




 /* 自写带缓存部分 */
#if 0

uint8_t buffer_rx_temp;
uint8_t Uart1_RxBuff[256];
uint8_t Uart1_Rx_Cnt = 0;
uint8_t cAlmStr[] = "数据溢出(>256)\r\n";

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
  if(huart->Instance==USART1){  
    if(Uart1_Rx_Cnt >= 255){
        Uart1_Rx_Cnt = 0;
	memset(Uart1_RxBuff,0x00,sizeof(Uart1_RxBuff));
        Uart_Printf("%s",cAlmStr);
        //HAL_UART_Transmit(&huart1,cAlmStr,strlen(cAlmStr),0xFFFF);
    }
    else {
        Uart1_RxBuff[Uart1_Rx_Cnt++] = buffer_rx_temp;   //接收数据转存
	if((Uart1_RxBuff[Uart1_Rx_Cnt-1] == 0x0A)&&(Uart1_RxBuff[Uart1_Rx_Cnt-2] == 0x0D)) //判断结束位
          {
            if(isAllNum(Uart1_RxBuff)){
              TIM2 ->ARR = atof(Uart1_RxBuff)*7200;
              Uart_Printf("Frequency Changed to %f",atof(Uart1_RxBuff));
                }    
             else Uart_Printf("Wrong Order!");
                //HAL_UART_Transmit(&huart1, (uint8_t *)&Uart1_RxBuff, Uart1_Rx_Cnt,0xFFFF); //将收到的信息发送出去
		Uart1_Rx_Cnt = 0;
		memset(Uart1_RxBuff,0x00,sizeof(Uart1_RxBuff)); //清空数组
          }
    }
    HAL_UART_Receive_IT(&huart1,(uint8_t *)&buffer_rx_temp,1);
  }
}

#endif

/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
